#ifndef MADSIMPLE_SDECODER_H
#define MADSIMPLE_SDECODER_H 1

#include<mad.h>
#include<pthread.h>

extern PyMethodDef SDecoder_methods[];
extern PyMemberDef SDecoder_members[];
extern PyTypeObject SDecoderType;

typedef struct {
	char *data;
	int cur_offset;
	int size;
	int allocated_size;
	int state;              // 0(writable) or 1(readable)
	int eof;                // true if data stream reached end.
	pthread_mutex_t mutex;
	pthread_cond_t cond;
#define WRITABLE 0
#define READABLE 1
} Data;

typedef struct {
	PyObject_HEAD
	PyObject *FileObject;
	//PyObject *buffer;
	//PyObject *first;
	//PyObject *last;
	//int number;
	struct mad_decoder decoder;
	pthread_t thread;
	char *buff;
	int bufsiz;

	Data *data[2];	// double buffered stream
	int writeto;
	int readfrom;
} SDecoder;

static inline void
initialize_Data(Data *data) {
	memset(data, 0, sizeof(Data));
	data->state = WRITABLE;
	//data->mutex = PTHREAD_MUTEX_INITIALIZER;
	//data->cond = PTHREAD_COND_INITIALIZER;
	pthread_mutex_init(&data->mutex, NULL);
	pthread_cond_init(&data->cond, NULL);
}

#endif //MADSIMPLE_SDECODER_H
